using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using Spread.Common.DataStore;
using FarPoint.Win.Spread.CellType;
using FarPoint.Win.Spread;
namespace Spread.CellTypes.Features
{
    public partial class Introduction : BaseSampleControl
    {
        public Introduction()
        {
            InitializeComponent();
            Name = "Introduction to Cell Types";
            SetupSpreadSheet();
            InitComboBox();
            InitCurrency();
            InitNumber();
            InitCheckBox();
            InitButton();

            spread.TextTipPolicy = TextTipPolicy.Floating;
            spread.ButtonClicked += Spread_ButtonClicked;
            spread.TextTipFetch += Spread_TextTipFetch;
        }

        private void SetupSpreadSheet()
        {
            introductionSheet.DataAutoSizeColumns = false;
            introductionSheet.DataAutoCellTypes = false;
            introductionSheet.Columns.Default.Width = 90;
            introductionSheet.Rows.Default.Height = 25;
            introductionSheet.Columns[2].Width = 200;
            introductionSheet.Columns[3].Width = 80;
            introductionSheet.DataSource = new DataService().AccessDataSource.GetNWINDData("Products");

            introductionSheet.Columns.Add(introductionSheet.ColumnCount, 1);
            introductionSheet.Columns[introductionSheet.ColumnCount - 1].Label = "Place Order";
        }
        private void InitComboBox()
        {
            ComboBoxCellType supplierComboBox = new ComboBoxCellType();

            supplierComboBox.Items = GetComboBoxData("CompanyName", "SupplierID", "Suppliers");
            supplierComboBox.ItemData = GetComboBoxData("SupplierID", "SupplierID", "Products");

            introductionSheet.Columns[2].CellType = supplierComboBox;
            supplierComboBox.EditorValue = EditorValue.ItemData;

            introductionSheet.ColumnHeader.Cells[0, 2].Note = "ComboBox CellType";
            introductionSheet.ColumnHeader.Cells[0, 2].NoteStyle = NoteStyle.PopupNote;
        }
        private void InitCurrency()
        {
            CurrencyCellType cellType = new CurrencyCellType();
            cellType.SpinButton = true;
            cellType.SpinIntegerIncrement = 1;
            cellType.SpinWrap = true;
            introductionSheet.Columns[5].CellType = cellType;
            introductionSheet.ColumnHeader.Cells[0, 5].Note = "Currency CellType";
            introductionSheet.ColumnHeader.Cells[0, 5].NoteStyle = NoteStyle.PopupNote;
        }
        private void InitNumber()
        {
            NumberCellType cellType = new NumberCellType();
            cellType.DecimalPlaces = 2;
            cellType.FractionMode = true;
            cellType.OverflowCharacter = '#';
            cellType.DropDownButton = true;
            cellType.SpinButton = true;

            introductionSheet.Columns[6].CellType = cellType;
            introductionSheet.ColumnHeader.Cells[0, 6].Note = "Number CellType";
            introductionSheet.ColumnHeader.Cells[0, 6].NoteStyle = NoteStyle.PopupNote;
        }
        private void InitCheckBox()
        {
            CheckBoxCellType checkBox = new CheckBoxCellType();
            checkBox.TextFalse = "Unchecked";
            checkBox.TextTrue = "Checked";

            introductionSheet.Columns[9].CellType = checkBox;
            introductionSheet.ColumnHeader.Cells[0, 9].Note = "CheckBox CellType";
            introductionSheet.ColumnHeader.Cells[0, 9].NoteStyle = NoteStyle.PopupNote;
        }
        private void InitButton()
        {
            ButtonCellType buttonCell = new ButtonCellType();
            buttonCell.Text = "Place Order";
            introductionSheet.Columns[introductionSheet.ColumnCount - 1].CellType = buttonCell;
        }
        private string[] GetComboBoxData(string selectColumn, string orderColumn, string tableName)
        {
            var data = new DataService().AccessDataSource.GetNWINDDataComplete(string.Format("SELECT DISTINCT {0},{2} FROM `{1}` ORDER BY {2} ASC", selectColumn, tableName, orderColumn));
            string dataItem = "";
            foreach (DataRow item in data.Tables[0].Rows)
            {
                dataItem += item[0] + "|";
            }
            return dataItem.TrimEnd('|').Split('|');
        }

        private void Spread_TextTipFetch(object sender, TextTipFetchEventArgs e)
        {
            if (e.Column == introductionSheet.ColumnCount - 1 && !e.ColumnHeader)
            {
                string productName = introductionSheet.Cells[e.Row, 1].Text;
                e.TipText = $"Place order for 10 units of {productName}";
                e.WrapText = true;
                e.ShowTip = true;
            }
        }

        private void Spread_ButtonClicked(object sender, EditorNotifyEventArgs e)
        {
            if (e.Column == introductionSheet.ColumnCount - 1)
            {
                introductionSheet.Cells[e.Row, 6].Value = 10 + Convert.ToInt32(introductionSheet.Cells[e.Row, 6].Value);
            }
        }
    }
}


//
//Introduction.Designer.cs
//


namespace Spread.CellTypes.Features
{
    partial class Introduction
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.spread = new FarPoint.Win.Spread.FpSpread();
            this.introductionSheet = new FarPoint.Win.Spread.SheetView();
            this.pnlMain.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.spread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.introductionSheet)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.spread);
            // 
            // spread
            // 
            this.spread.AccessibleDescription = "spread, Introduction, Row 0, Column 0";
            this.spread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.spread.Location = new System.Drawing.Point(0, 0);
            this.spread.Name = "spread";
            this.spread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.introductionSheet});
            this.spread.Size = new System.Drawing.Size(477, 487);
            this.spread.TabIndex = 0;
            // 
            // introductionSheet
            // 
            this.introductionSheet.Reset();
            this.introductionSheet.SheetName = "Introduction";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.introductionSheet.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.introductionSheet.RowHeader.Columns.Default.Width = 35F;
            this.introductionSheet.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // Introduction
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "Introduction";
            this.pnlMain.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.spread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.introductionSheet)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread spread;
        private FarPoint.Win.Spread.SheetView introductionSheet;
    }
}
